//
//  ActivitySingleton.m
//  UI_豆瓣项目
//
//  Created by dllo on 15/11/6.
//  Copyright (c) 2015年 关业达. All rights reserved.
//

#import "ActivitySingleton.h"
#import <sqlite3.h>

@implementation ActivitySingleton

+(instancetype)shareActivitySingleton
{
    static ActivitySingleton *activitySing = nil;
    if (activitySing == nil) {
        activitySing = [[ActivitySingleton alloc] init];
    }
    return activitySing;
}

// 声明数据库对象
static sqlite3 *ab = nil;

// 打开数据库
- (void)openAB
{
    if (ab != nil) {
        NSLog(@"数据库已经打开了");
        return;
    }
    NSString *sqlFile = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *sqlPath = [sqlFile stringByAppendingPathComponent:@"activity.sqlite"];
    
//    NSLog(@"sqlPath = %@", sqlPath);
    
    
    BOOL result = sqlite3_open(sqlPath.UTF8String, &ab);
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    }
    else{
        NSLog(@"打开失败");
    }
}



// 创建表
- (void)createTable
{
    NSString *sql = @"CREATE TABLE IF NOT EXISTS shoucang001(number INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, address TEXT, begin_time TEXT, end_time TEXT, category_name TEXT, participant_count TEXT, wisher_count TEXT, Aimage TEXT, movieId TEXT, name TEXT, content TEXT)";
    BOOL result = sqlite3_exec(ab, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }else{
        NSLog(@"创建失败");
    }
}

// 插入一个收藏
- (void)insertActivity:(Activity *)acitivity UserName:(NSString *)userName
{
    NSLog(@"userName%@", userName);
    NSString *sql = [NSString stringWithFormat:@"INSERT INTO shoucang001(title, address, begin_time, end_time, category_name, participant_count, wisher_count, Aimage, movieId, name, content) VALUES('%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@', '%@','%@')",acitivity.title, acitivity.address, acitivity.begin_time, acitivity.end_time, acitivity.category_name, acitivity.participant_count, acitivity.wisher_count, acitivity.Aimage, acitivity.movieId, acitivity.name, acitivity.content];
    BOOL result =  sqlite3_exec(ab, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }
    else {
        NSLog(@"插入失败");
    }
}

// 删除数据
- (void)deleteActivity:(NSString *)name
{
    NSString *sql = [NSString stringWithFormat:@"DELETE FROM shoucang001 WHERE name = '%@'", name];
    BOOL result = sqlite3_exec(ab, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }
    else{
        NSLog(@"删除失败");
    }
}

// 查询收藏
- (NSMutableArray *)selectActivity: (NSString *)name
{
    NSMutableArray *array = [NSMutableArray array];
    NSString *sql = @"SELECT * FROM shoucang001";
    sqlite3_stmt *stmt = nil;
    BOOL result = sqlite3_prepare(ab, sql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"查询准备成功");
    }else{
        NSLog(@"查询准备失败");
    }
    // 循环遍历每一行
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        const unsigned char *title = sqlite3_column_text(stmt, 1);
        const unsigned char *address = sqlite3_column_text(stmt, 2);
        const unsigned char *begin_time = sqlite3_column_text(stmt, 3);
        const unsigned char *end_time = sqlite3_column_text(stmt, 4);
        const unsigned char *category_name = sqlite3_column_text(stmt, 5);
        const unsigned char *participant_count = sqlite3_column_text(stmt, 6);
        const unsigned char *wisher_count = sqlite3_column_text(stmt, 7);
        const unsigned char *Aimage = sqlite3_column_text(stmt, 8);
        const unsigned char *movieId = sqlite3_column_text(stmt, 9);
        const unsigned char *name = sqlite3_column_text(stmt, 10);
        const unsigned char *content = sqlite3_column_text(stmt, 11);
        // 转化OC
        NSString *titleSter = [NSString stringWithUTF8String:(const char *) title];
        NSString *addressSter = [NSString stringWithUTF8String:(const char *) address];
        NSString *begin_timeSter = [NSString stringWithUTF8String:(const char *) begin_time];
        NSString *end_timeSter = [NSString stringWithUTF8String:(const char *) end_time];
        NSString *category_nameSter = [NSString stringWithUTF8String:(const char *) category_name];
        NSString *participant_countSter = [NSString stringWithUTF8String:(const char *) participant_count];
        NSString *wisher_countSter = [NSString stringWithUTF8String:(const char *) wisher_count];
        NSString *AimageSter = [NSString stringWithUTF8String:(const char *) Aimage];
        NSString *movieIdSter = [NSString stringWithUTF8String:(const char *) movieId];
        NSString *nameSter = [NSString stringWithUTF8String:(const char *) name];
        NSString *contentSter = [NSString stringWithUTF8String:(const char *) content];
        // 创建学生对象
        Activity *act = [[Activity alloc] init];
        act.title = titleSter;
        act.address = addressSter;
        act.begin_time = begin_timeSter;
        act.end_time = end_timeSter;
        act.category_name = category_nameSter;
        act.participant_count = participant_countSter;
        act.wisher_count = wisher_countSter;
        act.Aimage = AimageSter;
        act.movieId = movieIdSter;
        act.name = nameSter;
        act.content = contentSter;
        [array addObject:act];
    }
    return array;
}


- (void)dropTable
{
    NSString *sql = @"DROP TABLE shoucang001";
    BOOL result = sqlite3_exec(ab, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else{
        NSLog(@"删除失败");
    }
}





//@property (nonatomic, copy)NSString *title;
//@property (nonatomic, copy)NSString *address;
//@property (nonatomic, copy)NSString *begin_time;
//@property (nonatomic, copy)NSString *end_time;
//@property (nonatomic, copy)NSString *category_name; //类型
//@property (nonatomic, copy)NSString *participant_count; // 参加
//@property (nonatomic, copy)NSString *wisher_count;// 感兴趣
//@property (nonatomic, copy)NSString *Aimage;
//@property (nonatomic, copy)NSString *movieId;
//@property (nonatomic, copy)NSString *name;
//@property (nonatomic, copy)NSString *content;







@end
